package com.s_recycler;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.s7.recycler.layout.SmartRefreshLayout;
import com.s7.recycler.layout.api.RefreshFooter;
import com.s7.recycler.layout.api.RefreshHeader;
import com.s7.recycler.layout.api.RefreshLayout;
import com.s7.recycler.layout.header.ClassicsFooter;
import com.s7.recycler.layout.header.ClassicsHeader;
import com.s7.recycler.layout.impl.DefaultRefreshFooterCreator;
import com.s7.recycler.layout.impl.DefaultRefreshHeaderCreator;
import com.s7.recycler.layout.impl.DefaultRefreshInitializer;
import com.s7.recycler.layout.util.DynamicTimeFormat;

public class MyApp extends Application {

    private static MyApp instance;
    private RequestOptions options;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.NONE);

        //设置全局默认配置（优先级最低，会被其他设置覆盖）
        SmartRefreshLayout.setDefaultRefreshInitializer(new DefaultRefreshInitializer() {
            @Override
            public void initialize(@NonNull Context context, @NonNull RefreshLayout layout) {
                //全局设置（优先级最低）
                layout.setEnableAutoLoadMore(true);
                layout.setEnableOverScrollDrag(false);
                layout.setEnableOverScrollBounce(true);
                layout.setEnableLoadMoreWhenContentNotFull(true);
                layout.setEnableScrollContentWhenRefreshed(true);
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
                layout.getLayout().setTag("close egg");
            }
        });
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                //全局设置主题颜色（优先级第二低，可以覆盖 DefaultRefreshInitializer 的配置，
                // 与下面的ClassicsHeader绑定）
                return new ClassicsHeader(context).setTimeFormat(new DynamicTimeFormat("更新于 %s"));
            }
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return  new ClassicsFooter(context);
            }
        });
    }

    public static MyApp getInstance() {
        return instance;
    }

    /**
     * 设置网络图片
     * @param url
     * @param iv
     */
    public void setImage(String url, ImageView iv) {

        Glide.with(this)
                .load(url)
                .apply(options)
                .into(iv);



//        Picasso.with(getInstance())
//                .load(url)
//                .placeholder(R.mipmap.ic_launcher)// 默认显示图片
//                .error(R.mipmap.ic_launcher)// 加载时出现错误显示的图片
//                .fit() //  计算出最佳的大小及最佳的图片质量来进行图片展示 (  减少内存 )
//                .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) // 查看大图放弃缓存，加速内存的回收
////                .rotate(90f) //旋转90度
//                .into(iv);
    }

    /**
     * 设置网络图片--瀑布流
     * @param url
     * @param iv
     */
    public void setStaggeredImage(String url, final ImageView iv) {
        Glide.with(this)
                .asBitmap()
                .load(url)
                .apply(options)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        ViewGroup.LayoutParams lp = iv.getLayoutParams();
                        int ivWidth = (int) getResources().getDimension(R.dimen.load_image);
                        int height = resource.getWidth() * resource.getHeight() / ivWidth;
                        Log.d("瀑布流尺寸", "图片 w=" + resource.getWidth()
                                + " h=" + resource.getHeight() + ";" +
                                " 容器 w=" + ivWidth + " h=" + lp.height);
                        if (height > ivWidth * 5) {
                            lp.height = (int) (ivWidth * 1.5);
                        }
                        if (height < ivWidth * 3) {
                            lp.height = (int) (ivWidth * 1.1);
                        }
                        iv.setLayoutParams(lp);
                        iv.setImageBitmap(resource);
                    }
                });
    }
}
